<template>
  <div class="import-read-dialog">
    <MyDialog :show="isImportRead" @beforeClose="beforeClose">
      <template #body>
        <el-form>
          <el-form-item label="下载导入模版：">
            <div class="model">
<!-- href='https://worker.qcdoc.cn/static/box.xlsx' @click="downExcel" -->
              <a download="下载" href='/box.xlsx' >xlsx文件模版</a>
            </div>
          </el-form-item>
          <el-form-item label="上传xlsx文件：">
            <el-button type="info" size="small" v-if="isShowRemind" @click="showRemind">选择文件</el-button>
            <el-upload
              v-else
              class="upload-demo"
              action
              ref="ImportUpload"
              accept=".xlsx"
              :http-request="modeUpload"
              :before-upload="beforeUpload"
              :on-change="handleTopImageSuccess"
              :on-remove="handleRemove"
              :limit="1"
            >
              <el-button type="info" size="small">选择文件</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </template>
      <div slot="title">
        <p>{{ title }}</p>
      </div>
      <div slot="footer">
        <span>
          <el-button size="small" @click="closeDialog">取 消</el-button>
          <el-button size="small" type="primary" @click="submitImport"
            >确 定</el-button
          >
        </span>
      </div>
    </MyDialog>

    <UploadRemind :visible="dialogVisible" @close="handleClose" @submit="handleSubmit" :info="info"></UploadRemind>
  </div>
</template>

<script>
import { getRemindInfo } from '@/api/access-manage/index'
import UploadRemind from '@/components/uploadRemind.vue';

export default {
  name: "ImportReadDialog",
  props: {
    isImportRead: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "批量导入调阅",
    },
  },
  components: {
    UploadRemind
  },
  watch: {
    isImportRead() {
      if (this.isImportRead) {
        if (this.isPrivateLocalF) {
          this.judgeShowRemind()
          this.isShowRemind = true
        }
      }
      if (!this.isImportRead) {
        this.$refs.ImportUpload.clearFiles();
        this.$emit("modeUpload", {});
      }
    },
  },
  data() {
    return {
      isPrivateLocalF: process.env.VUE_APP_LOCAL == 'fangshan',
      info: '上传附件提醒',
      dialogVisible: false, // 上传附件提示框
      isShowRemind: false,
    }
  },
  methods: {
    // 调用接口，判断是否需要展示提示按钮， 有值,将值复制到框里
    judgeShowRemind() {
      // 接口
      getRemindInfo().then(res => {
        this.info = res.data
        if(res.data == null) {
          this.isShowRemind = false
        }
      })

    },
    // 展示上传文件提示框
    showRemind() {
      this.dialogVisible = true
    },
    handleSubmit() {
      this.dialogVisible = false
      this.isShowRemind = false
    },
    handleClose() {
      this.dialogVisible = false
    },

    // 导入- 确认
    submitImport() {
      this.$emit("submitImport");
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit("closeImportDialog");
    },
    // 关闭弹窗 右侧X
    beforeClose() {
      this.$emit("closeImportDialog");
    },
    // 文件上传
    modeUpload(param) {
      this.$emit("modeUpload", param);
    },
    // 文件移除
    handleRemove(file) {
      this.$refs.ImportUpload.clearFiles();
      this.$emit("modeUpload", {});
    },
    // 上传文件限制
    beforeUpload(file) {
      // 限制文件最大为10MB
      const isLt10M = file.size / 1024 / 1024 <= 10;
      if(!isLt10M) {this.$message.warning('上传文件大小不能超过 10MB!');}

      return isLt10M;
    },
    // 上传成功后
    handleTopImageSuccess() {
      // this.$refs.uploadButton.submit();
      // // 确定上传后需 清空文件 否则会多次上传
      // this.$refs.ImportUpload.clearFiles();
    },
  },
};
</script>

<style lang="scss" scoped></style>
